/* smog_pollution_placebo.do: This script runs a placebo test of a sort, regressing pollution on
leads and lags of re-inspections, most of which should not directly affect pollution.  
This produces Figure 2 of the paper*/

capture log close

clear all

set more off
set matsize 4000

cd ${path}analysis

log using smog_pollution_placebo.log, replace


use pollution_star_merged

xtset county date

/*First, divide the counts of inspections by 1000, to make the coefficients easier to read*/

gen lag = 0
foreach var of varlist /*star* re_pass* directed* asm**/ *pass* firstgen* othergen* {
	qui replace `var' = (`var')/1000
}

/* Generate day-of-week and calendar week*/

gen dow = dow(date)
gen week = wofd(date)

/*Declare the "if" conditions in a local, as well as the full set of controls*/

local conditions  if year>=1998 & inlist(county,5,12,18,22,26,53)==0 & (inlist(county,3,6,11,12,13,17,23,47,58)==0 | (year !=2002&year!=2003)) 
local controls tMin tMax prec ib1.county ib1.county#i.year /*ib1.county#(c.date##c.date##c.date)*/ i.week 


xtset county date

/* Create macros so the coefficients plot nicely using coefplot*/
global bin 90
local offset 5

global relocate firstgen_${bin}=-`offset' othergen_${bin}= `offset'
global critlist firstgen_${bin} othergen_${bin}
global oldkeep firstgen_${bin}
global newkeep othergen_${bin}
forvalues i = $bin(${bin})270 {

	local old =  `i' - `offset'
	local new =  `i' + `offset'
	global relocate $relocate L`i'.firstgen_${bin} = `old'  L`i'.othergen_${bin} = `new' 
	
	local old =  -`i' - `offset'
	local new =  -`i' + `offset' 
	global relocate $relocate F`i'.firstgen_${bin}=`old'   F`i'.othergen_${bin}=`new'
	
	global critlist $critlist L`i'.firstgen_${bin}  F`i'.firstgen_${bin}  L`i'.othergen_${bin}  F`i'.othergen_${bin}
	global oldkeep $oldkeep L`i'.firstgen_${bin} F`i'.firstgen_${bin}
	global newkeep $newkeep L`i'.othergen_${bin} F`i'.othergen_${bin}
}
	
/*Now run regressions for each pollutant*/

foreach pollutant in nox /*ozone */ co {

	/*Multiply the pollutant variables by 1000 to change to ppb (again, to make coefficients readable*/

	replace `pollutant' = `pollutant'*1000

	eststo `pollutant'_event_lags: regress `pollutant'  F(${bin}(${bin})270).firstgen_$bin F(${bin}(${bin})270).othergen_${bin} L(0(${bin})270).firstgen_${bin} L(0(${bin})270).othergen_${bin} `controls'  `conditions', vce(cluster county)

}

/* Determine uniform sup-t critical values for the confidence intervals*/
set seed 11710
foreach spec in co_event_lags nox_event_lags {
	disp "`spec'"
	qui estimates restore `spec'
	st_crit $critlist
	global `spec'_crit = r(crit)
}

/* Figure 2: Plot "event study" lead and lag coefficients*/

#delimit ;
coefplot (co_event_lags, keep($oldkeep) recast(connect) lcolor(gs10) mcolor(gs10) msize(small) levels(${co_event_lags_crit}) ciopts(lcolor(gs12)))
(co_event_lags, keep($newkeep) recast(connect) lcolor(gs2) mcolor(gs2) msize(small) msymbol(S) levels(${co_event_lags_crit}) ciopts(lcolor(gs4))), 
vertical xline(0)
legend(label(2 "1975-1985 Model Years") label(4 "1985+ Model Years")) xtitle(Days after pollution reading) 
ytitle("Effect of 1000 Re-inspections on CO (PPB)") relocate($relocate)  xlabel(-270(90)270) xscale(range(-290 290)) graphregion(color(white))
;
#delimit cr
graph export co_event_lags.pdf, replace

#delimit ;
coefplot (nox_event_lags, keep($oldkeep) recast(connect) lcolor(gs10) mcolor(gs10) msize(small) levels(${nox_event_lags_crit}) ciopts(lcolor(gs12)))
(nox_event_lags, keep($newkeep) recast(connect) lcolor(gs2) mcolor(gs2) msize(small) msymbol(S) levels(${nox_event_lags_crit}) ciopts(lcolor(gs4))), 
vertical xline(0)
legend(label(2 "1975-1985 Model Years") label(4 "1985+ Model Years")) xtitle(Days after pollution reading) 
ytitle("Effect of 1000 Re-inspections on NOx (PPB)") relocate($relocate)  xlabel(-270(90)270)  xscale(range(-290 290)) graphregion(color(white))
;
#delimit cr

graph export nox_event_lags.pdf, replace

#delimit ;

